<template>
  <div id="departments">
    <div class="card">
      <div class="card-header bg-info text-white">部门信息</div>
      <div class="card-body">
        <form class="row">
          <div class="col-1">
            <input
              type="textfield"
              class="form-control"
              placeholder="部门名称"
              name="sc.name"
              v-model.trim="searchName"
              size="15"
            />
          </div>
          <div class="col-1 ms-1">
            <select class="form-select" v-model.number="sc.pageSize">
              <option value="10">10</option>
              <option value="25">25</option>
              <option value="50">50</option>
              <option value="100">100</option>
              <option value="500">500</option>
            </select>
          </div>
          <div class="col">
            <button
              type="button"
              class="btn btn-primary ms-2"
              @click="search()"
            >
              查找
            </button>
            <button
              type="button"
              class="btn btn-secondary ms-2"
              title="清空查找条件"
              @click="reset()"
            >
              重置
            </button>

            <a
              href="javascript:void(0)"
              v-on:click="syncDept()"
              class="btn btn-secondary me-5 float-end"
              title="把企业微信中的部门信息下载到系统中"
              >同步</a
            >
            <a
              href="javascript:void(0)"
              v-on:click="newDept()"
              class="btn btn-success me-5 float-end"
              >新建</a
            >
          </div>
        </form>
      </div>
    </div>
    <table class="table table-striped table-sm">
      <thead>
        <tr>
          <th>部门</th>
          <th>角色</th>
          <th>部门ID</th>
          <th>上级部门ID</th>
          <th>备注</th>
          <th>创建时间</th>
          <th>最后修改时间</th>
          <th></th>
          <th></th>
          <th></th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(info, index) in dataList" :key="index">
          <td>{{ info.name }}</td>
          <td>{{ info.roleName }}</td>
          <td>{{ info.deptId }}</td>
          <td>{{ info.parentId }}</td>
          <td>{{ info.remark }}</td>
          <td>{{ info.createTime }}</td>
          <td>{{ info.lastUpdate }}</td>
          <td>
            <a href="javascript:void(0)" v-on:click="syncDeptUsers(info.deptId)"
              >获取成员</a
            >
          </td>
          <td>
            <a href="javascript:void(0)" v-on:click="editDept(index)">修改</a>
          </td>
          <td>
            <a
              href="javascript:void(0)"
              v-on:click="deleteDept(info.id)"
              class="text-danger"
              >删除</a
            >
          </td>
        </tr>
      </tbody>
    </table>
    <nav id="pagination-box" class="float-end">
      <my-pagination
        name="pagination"
        :row-count="sc.rowCount"
        :page-total="sc.pageTotal"
        :page-no="sc.pageNo"
        @next-page="nextPage"
        @prev-page="prevPage"
        @direct-page="directPage"
      ></my-pagination>
    </nav>

    <my-modal-department ref="departmentModal" :roles="roles"> </my-modal-department>
  </div>
</template>

<script>
import { mapMutations } from "vuex";
import {
  searchDepartments,
  createDepartment,
  deleteDepartment,
  syncDeptUsers,
  syncDepts,
} from "@/api/admin.js";
import { searchRoles } from "@/api/groups.js";
import $ from "jquery";
import MyPagination from "@/components/my-pagination.vue";
import MyModalDepartment from "@/components/modal/my-modal-department.vue";

export default {
  components: {
    MyPagination,
    MyModalDepartment,
  },
  data() {
    return {
      id: 0,
      name: "",
      role: "",
      parentId: 0,
      wwDeptId: 0,
      remark: "",

      dataList: [],
      roles: [],

      sc: {
        rowCount: 0,
        pageNo: 1,
        pageSize: 10,
        pageTotal: 0,
      },
      searchName: "",
    };
  },
  computed: {
    isAdmin() {
      return this.$store.getters.isAdmin;
    },
  },
  mounted: function () {
    if (window.matchMedia("(max-width: 576px)").matches) {
      console.log(this.$router);
      this.$router.replace("/h5/admin/depts");
    } else {
      this.search();
      this.searchRoles();
    }
  },
  methods: {
    ...mapMutations(["showLoading", "hideLoading"]),
    showErrMsg: function (msg, msgType) {
      this.$store.dispatch("showAlertMsg", {
        errMsg: msg,
        errMsgType: msgType,
      });
    },
    search: function () {
      const params = {
        "sc.pageNo": this.sc.pageNo,
        "sc.pageSize": this.sc.pageSize,
        "sc.name": this.searchName,
      };
      // this.showLoading('查询中...')
      searchDepartments(params, (jsonResult) => {
        this.dataList = jsonResult.dataList;
        this.sc = jsonResult.page;
        // this.hideLoading()
      });
    },
    newDept: function () {
      this.id = 0;
      this.name = "";
      this.role = "";
      this.parentId = 1;
      this.wwDeptId = 0;
      this.remark = "";

      const v = {
        id: this.id,
        wwDeptId: this.wwDeptId,
        name: this.name,
        role: this.role,
        parentId: this.parentId,
        remark: this.remark,
      };

      this.$refs.departmentModal
        .modal(v)
        .then((v) => {this.saveDept(v)})
        .then((ex) => {});
    },
    saveDept: function (v) {
      if (v.parentId < 1) {
        v.parentId = 1;
      }
      const params = {
        id: v.id,
        wwDeptId: v.wwDeptId,
        name: v.name,
        role: v.role,
        parentId: v.parentId,
        remark: v.remark,
      };

      // const jsonParam = JSON.stringify(params)

      createDepartment(params, (jsonResult) => {
        if (jsonResult.status !== "OK") {
          this.showErrMsg(jsonResult.errmsg, "danger");
        } else {
          this.showErrMsg("操作完成");
          this.search();
        }
      });
    },
    deleteDept: function (id) {
      const params = {
        id: id,
      };

      deleteDepartment(params, (jsonResult) => {
        if (jsonResult.status !== "OK") {
          this.showErrMsg("操作失败: " + jsonResult.errmsg, "danger");
        } else {
          this.showErrMsg("操作成功");
          this.search();
        }
      });
    },
    editDept: function (index) {
      const info = this.dataList[index];

      this.id = info.id;
      this.wwDeptId = info.deptId;
      this.name = info.name;
      this.role = info.role;
      this.parentId = info.parentId;
      this.remark = info.remark;

      const v = {
        id: this.id,
        wwDeptId: this.wwDeptId,
        name: this.name,
        role: this.role,
        parentId: this.parentId,
        remark: this.remark,
      };

      this.$refs.departmentModal
        .modal(v)
        .then((v) => {this.saveDept(v)})
        .then((ex) => {});
    },
    syncDeptUsers: function (id) {
      this.showLoading();
      syncDeptUsers(
        id,
        (v) => {
          if (v.status !== "OK") {
            this.showErrMsg("操作失败: " + v.errmsg, "danger");
          } else {
            this.showErrMsg("操作成功");
            this.search();
          }
        },
        () => {
          this.hideLoading();
        }
      );
    },
    syncDept: function () {
      this.showLoading();
      syncDepts(
        (v) => {
          if (v.status === "OK") {
            this.showErrMsg("同步成功");
          } else {
            this.showErrMsg(v.errmsg, "danger");
          }
        },
        () => this.hideLoading()
      );
    },
    reset: function () {
      this.name = "";
      this.sc.pageNo = 1;
      this.search();
    },
    searchRoles: function () {
      if (this.roles.length === 0) {
        searchRoles((jsonResult) => {
          this.roles = jsonResult;
        });
      }
    },
    prevPage: function () {
      this.sc.pageNo = this.sc.pageNo - 1;
      if (this.sc.pageNo < 1) this.sc.pageNo = 1;
      this.search();
    },
    nextPage: function () {
      this.sc.pageNo = this.sc.pageNo + 1;
      this.search();
    },
    directPage: function (pageNo) {
      this.sc.pageNo = pageNo;
      this.search();
    },
  },
};
</script>
